<?php

namespace app\admin\model;

use think\Model;

class Order extends BaseModel
{
    protected $attributes = [
        'source' => [
            0 => '后台系统ERP',
            1 => '前端网站',
            2 => 'Shopify',
            3 => '聚水潭',
            4 => '速卖通',
            5 => '淘宝/天猫',
            6 => '闲鱼',
            7 => 'Yandex',
            8 => 'Ozon',
            9 => 'Shopee',
            10 => 'LAZADA',
            11 => 'Tiktok',
            12 => 'Newegg',
            13 => 'Amazon',
            14 => 'Ebay',
        ],
        'order_type' => [
            '1' => '用户订单',
            '2' => '经销商发货订单',
        ],
        'costing' => [
            '入仓费' => '入仓费',
            '出仓费' => '出仓费',
            '过货费' => '过货费',
            '国内运费' => '国内运费',
            '国际运费' => '国际运费',
            '报关费' => '报关费',
            '退回运费' => '退回运费',
            '操作费' => '操作费',
            '停车费' => '停车费',
            '偏远费' => '偏远费',
            '调拨费' => '调拨费',
            '其他费' => '其他费',
            '罚金' => '罚金',
            '税费' => '税费',
            '付私费用' => '付私费用'
        ],
        'delivery_method' => [
            0 => '自提',
            1 => '快递',
            2 => '仓内过货'
        ],
        'pay_terms' => [
            0 => '全款',
            1 => '预付款'
        ],
        'pay_status' => [
            1 => '未支付',
            2 => '待审核',
            3 => '已支付',
            4 => '部分支付'
        ],
        'status' => [
            10 => '待支付',  // 前端显示 待支付
            20 => '审核中', // 前端显示 审核中
            21 => '审核通过', // 前端显示 待支付
            30 => '待发货', // 前端显示 待发货
            40 => '部分发货',  // 前端显示 待收货
            50 => '待收货',  // 前端显示 待收货
            70 => '已取消',  // 前端显示 已取消
            100 => '已完成'  // 前端显示 已完成
        ],
        'reviewer_status' => [
            1 => '无需审核',
            2 => '审核中',
            3 => '审核通过'
        ],
        'pay_method' => [
            // 1 => 'Coinpal Pay',
            // 2 => 'Coinbase Pay',
            3 => 'Wire Transfer',
            10 => '现金'
        ],
        'prescription' => [
            1 => '普快',
            // 2 => '特快'
        ],
        'is_futures' => [
            0 => '现货',
            1 => '在途期货',
            2 => '大期货'
        ],
        'review_node' => [
            1 => '库存不足',
            2 => '订单更新',
            3 => '收货地址审核',
            4 => '创建订单',
            5 => '运费为空,未匹配到运费规则'
        ],
        'item_pay_status' => [
            '00' => '待审核',
            '01' => '支付成功',
            '02' => '支付失败',
            '03' => '待支付',
            '04' => '驳回'
        ],
        'single_order_method' => [
            1 => '独立成单',
            2 => '合作成单',
            3 => '渠道成单'
        ],
        'contract_type' => [
            1 => '国内销售合同',
            2 => '国外销售合同'
        ],
        'contract_signing_status' => [
            0 => '未签署',
            1 => '已签署'
        ],
        'delivery_terms' => [
            1 => 'DDP',
            2 => 'DDU'
        ],
        'exceptionstatus' => [
            1 => '库存不足',
            2 => '未匹配到物流规则',
            3 => '商品/库存同步错误',
            4 => '批次库存可转数量不足'
        ],
        'transfer_payment_info' => [
            1 => 'HK-USD',
            2 => 'HK-EUR',
            3 => 'HK-HKD',
            4 => '豪利杰USD',
            5 => 'TRC20:USDT',
            6 => 'ERC20:USDT',
            7 => 'ERC20:USDC',
            8 => 'BEP20:BUSD(Binance Smart Chain)',
        ],
        'aftersales_status' => [
            1 => '退款审批中',
            2 => '退货退款审批中',
            3 => '退货中',
            4 => '已退货',
            8 => '退款中',
            16 => '已退款',
            32 => '已退货 已退款'
        ],
        'transfer_payment_info_detail' => [
            1 => [
                'detail_label' => 'HK-USD',
                'detail_value' => "Beneficiary Bank: PING AN BANK CO., LTD.
Beneficiary's Bank Address: 5047, SHENNAN EAST ROAD, SHENZHEN 518001 P.R.CHINA
Beneficiary's Bank Swift: SZDBCNBS (If you need 11-digit SWIFT Code: SZDBCNBSXXX)
Beneficiary's Account Number:
OSA15000112465560(USD)
Remittance Information: Purchase of goods
Beneficiary's Name: SUNRAYS INTERNATIONAL GROUP LIMITED
Beneficiary's Address: FLAT/RM 63 07/F WOON LEE COMMERCIAL BUILDING 7-9 AUSTIN AVE TSIM SHA TUSI KL HK,ZIP CODE:999077 【Must be filled in on the Remittance Slip accurately】",
                'profile_label' => '香港明曦公司资料',
                'profile_value' => "Sunrays International Group Limited
ROOM 63,7/F, WOON LEE COMMERCIAL BUILDING, 7-9 AUSTIN AVE, TSIM SHA TSUI, KOWLOON, HONG KONG, 999077"
            ],
            2 => [
                'detail_label' => 'HK-EUR',
                'detail_value' => "Beneficiary Bank: PING AN BANK CO., LTD.
Beneficiary's Bank Address: 5047, SHENNAN EAST ROAD, SHENZHEN 518001 P.R.CHINA
Beneficiary's Bank Swift: SZDBCNBS (If you need 11-digit SWIFT Code: SZDBCNBSXXX)
Beneficiary's Account Number:
OSA15000112465366(EUR)
Remittance Information: Purchase of goods
Beneficiary's Name: SUNRAYS INTERNATIONAL GROUP LIMITED
Beneficiary's Address: FLAT/RM 63 07/F WOON LEE COMMERCIAL BUILDING 7-9 AUSTIN AVE TSIM SHA TUSI KL HK,ZIP CODE:999077【Must be filled in on the Remittance Slip accurately】",
                'profile_label' => '香港明曦公司资料',
                'profile_value' => "Sunrays International Group Limited
ROOM 63,7/F, WOON LEE COMMERCIAL BUILDING, 7-9 AUSTIN AVE, TSIM SHA TSUI, KOWLOON, HONG KONG, 999077"
            ],
            3 => [
                'detail_label' => 'HK-HKD',
                'detail_value' => "Beneficiary Bank: PING AN BANK CO., LTD.
Beneficiary's Bank Address: 5047, SHENNAN EAST ROAD, SHENZHEN 518001 P.R.CHINA
Beneficiary's Bank Swift: SZDBCNBS (If you need 11-digit SWIFT Code: SZDBCNBSXXX)
Beneficiary's Account Number:
OSA15000112465463(HKD)
Remittance Information: Purchase of goods
Beneficiary's Name: SUNRAYS INTERNATIONAL GROUP LIMITED
Beneficiary's Address: FLAT/RM 63 07/F WOON LEE COMMERCIAL BUILDING 7-9 AUSTIN AVE TSIM SHA TUSI KL HK,ZIP CODE:999077【Must be filled in on the Remittance Slip accurately】",
                'profile_label' => '香港明曦公司资料',
                'profile_value' => "Sunrays International Group Limited
ROOM 63,7/F, WOON LEE COMMERCIAL BUILDING, 7-9 AUSTIN AVE, TSIM SHA TSUI, KOWLOON, HONG KONG, 999077"
            ],
            4 => [
                'detail_label' => '豪利杰USD',
                'detail_value' => "Beneficiary Bank:   BANK OF COMMUNICATIONS SHANGHAI BRANCH YANGPU SUB-BRANCH
Address:  10TH FL. 99 ZHONG SHAN SOUTH RD. SHANGHAI 200010P.R. CHINA
Swift :  COMMCNSHSHI
Account Number: 310066690146300006278

Beneficiary:SHANGHAI COURAGE INTERNATIONAL TRADE CO. LTD
Address: R09, 4F, BLOCK 4NO.546 YUYUAN ROAD, 200040SHANGHAI CHINA ",
                'profile_label' => '豪利杰公司资料',
                'profile_value' => "PS.
Shanghai Courage International Trade Co.,Ltd is entrusted by Sunrays Info Tech Limited to receive the payment of this invoice."
            ],
            5 => [
                'detail_label' => 'TRC20:USDT',
                'detail_value' => "USDT Deposit Address(TRC20):
TQppsbYu9fVRruyYWshirC614X8ooGXchb ",
                'profile_label' => '香港明曦公司资料',
                'profile_value' => "Sunrays International Group Limited
ROOM 63,7/F, WOON LEE COMMERCIAL BUILDING, 7-9 AUSTIN AVE, TSIM SHA TSUI, KOWLOON, HONG KONG, 999077"
            ],
            6 => [
                'detail_label' => 'ERC20:USDT',
                'detail_value' => "USDT Deposit Address(ERC20):
0xCF5faeE1BE31f49106bE99d0eEed20866826De52",
                'profile_value' => "Sunrays International Group Limited
ROOM 63,7/F, WOON LEE COMMERCIAL BUILDING, 7-9 AUSTIN AVE, TSIM SHA TSUI, KOWLOON, HONG KONG, 999077"
            ],
            7 => [
                'detail_label' => 'ERC20:USDC',
                'detail_value' => "USDC Deposit Address(ERC20):
0xCF5faeE1BE31f49106bE99d0eEed20866826De52",
                'profile_label' => '香港明曦公司资料',
                'profile_value' => "Sunrays International Group Limited
ROOM 63,7/F, WOON LEE COMMERCIAL BUILDING, 7-9 AUSTIN AVE, TSIM SHA TSUI, KOWLOON, HONG KONG, 999077"
            ],
            8 => [
                'detail_label' => 'BEP20:BUSD(Binance Smart Chain)',
                'detail_value' => "BUSD Deposit Address(BEP20):
0xd2734dbcf245514d1c3987bb3648db4004d0c532",
                'profile_label' => '香港明曦公司资料',
                'profile_value' => "Sunrays International Group Limited
ROOM 63,7/F, WOON LEE COMMERCIAL BUILDING, 7-9 AUSTIN AVE, TSIM SHA TSUI, KOWLOON, HONG KONG, 999077"
            ],
        ]
    ];

    public  function getStaticAttr()
    {
        return $this->attributes;
    }

    protected $enumCallbacks = [
        'operator' => 'getOperatorEnum',
        'currency' => 'getCurrencyEnum',
        'brand' => 'getBrandEnum',
        'warehouse' => 'getWarehouseEnum',
        'logistics_company' => 'getLogisticsCompanyEnum',
        'logistics_company_tree' => 'getAllCompanies'
    ];

    protected function getAllCompanies()
    {
        $oLogisticsCompany = new LogisticsCompany();
        $companies = $oLogisticsCompany->getAllCompanies();
        $processedData = $oLogisticsCompany->processCompanyData($companies);
        return $processedData['tree'];
    }


    public function getBrandEnum()
    {
        return ProductBrand::where('is_del', 0)->column('id as value,name as label');
    }

    public function getLogisticsCompanyEnum()
    {
        return LogisticsCompany::where('is_del', 0)->where('status', 1)->column('id as value,name as label');
    }

    public function getWarehouseEnum()
    {
        return Warehouse::where('is_del', 0)->column('id as value,name as label');
    }

    public function getOperatorEnum()
    {
        return Operator::where('is_del', 0)->column('id as value,cname as label');
    }

    public function getCurrencyEnum()
    {
        return TransactionCurrency::where([
            'status' => 1,
            'is_del' => 0,
        ])->column('id AS value,name as label');
    }

    public function getValueAttr($value, $data)
    {
        return $value . '';
    }

    public function getUserName()
    {
        return $this->belongsTo(Operator::class, 'user_id', 'id')->bind(['user_id_value' => 'nickname']);
    }

    public function getOrderTypeValueAttr($value, $data)
    {
        return isset($this->attributes['order_type'][$data['order_type']]) ? $this->attributes['order_type'][$data['order_type']] : '';
    }

    public function getDeliveryMethodsValueAttr($value, $data)
    {
        return isset($this->attributes['delivery_method'][$data['delivery_method']]) ? $this->attributes['delivery_method'][$data['delivery_method']] : '';
    }

    public function getPayTermsValueAttr($value, $data)
    {
        return isset($this->attributes['pay_terms'][$data['pay_terms']]) ? $this->attributes['pay_terms'][$data['pay_terms']] : '';
    }

    public function getPayStatusValueAttr($value, $data)
    {
        return isset($this->attributes['pay_status'][$data['pay_status']]) ? $this->attributes['pay_status'][$data['pay_status']] : '';
    }

    public function getStatusValueAttr($value, $data)
    {
        return isset($this->attributes['status'][$data['status']]) ? $this->attributes['status'][$data['status']] : '';
    }

    public function getReviewerStatusValueAttr($value, $data)
    {
        return isset($this->attributes['reviewer_status'][$data['reviewer_status']]) ? $this->attributes['reviewer_status'][$data['reviewer_status']] : '';
    }

    public function getPayMethodIdValueAttr($value, $data)
    {
        return isset($this->attributes['pay_method'][$data['payment_method']]) ? $this->attributes['pay_method'][$data['payment_method']] : '';
    }

    public function getOwnerValue()
    {
        return $this->belongsTo(Operator::class, 'owner', 'id')->bind(['owner_value' => 'cname']);
    }

    public function getPartnerValue()
    {
        return $this->belongsTo(Operator::class, 'partner', 'id')->bind(['partner_value' => 'cname']);
    }

    // public function getPartnerAttr($value, $data)
    // {
    //     return intval($value);
    // }

    public function getShippingAddressAttr($value, $data)
    {
        return $data['country_name'] . ' ' . $data['state'] . ' ' . $data['city_name'] . ' ' . $data['address'] . ' ' . $data['address2'] . ' ' . $data['company'];
    }

    public function goods()
    {
        return $this->hasMany(OrderItem::class, 'order_id', 'id')->field(['order_id', 'product_id', 'quantity as num', 'id as item_id', 'warehouse_id', 'inventory_id'])->with(['product', 'warehouse', 'invenroty']);
    }

    public function items()
    {
        return $this->hasMany(OrderItem::class, 'order_id', 'id')->where('is_del', 0)->with(['product_for_order', 'warehouses']);
    }

    public function payment()
    {
        return $this->hasMany(OrderPayment::class, 'order_id', 'id')->where('order_type', '=', 'order')->where('pay_type', '=', '00');
    }

    public function logistics()
    {
        return $this->hasMany(LogisticsOrderItem::class, 'order_id', 'id');
    }

    public function customer()
    {
        return $this->belongsTo(Customer::class, 'user_id', 'id')->bind(['user' => 'cusname', 'user_email' => 'email', 'role']);
    }

    public function creator()
    {
        return $this->belongsTo(Operator::class, 'creator', 'id')->bind(['creator_name' => 'cname']);
    }
    public function updater()
    {
        return $this->belongsTo(Operator::class, 'updater', 'id')->bind(['updater_name' => 'cname']);
    }

    public function logger()
    {
        $field = ['operate as title', 'content as info', 'create_time', 'entity_id'];
        return $this->hasMany(BusinessLog::class, 'entity_id', 'id')->where('business_module', 'order')->field($field)->order('create_time', 'asc');
    }


    /**
     *  关联明细
     */
    public function itemList()
    {
        return $this->hasMany(OrderItem::class, 'order_id', 'id')->where('is_del', 0)->with(['warehouse', 'product']);
    }


    public function getFieldEnum($field, $label = 'label')
    {
        $result = $this->getEnum($field, [], $label);

        return $result;
    }
}
